package com.zhiyizhen.handheld.component.execute;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zhiyizhen.handheld.common.DataCache;
import com.zhiyizhen.handheld.common.RecyclerViewUtil;
import com.zhiyizhen.handheld.common.RxJavaUtil;
import com.zhiyizhen.handheld.common.event.AddNewRecordEvent;
import com.zhiyizhen.handheld.common.view.BaseFragment;
import com.zhiyizhen.handheld.component.execute.adapter.ChargeRecordAdapter;
import com.zhiyizhen.handheld.component.execute.api.IExecuteService;
import com.zhiyizhen.handheld.component.execute.bean.ChargeRecordBean;
import com.zhiyizhen.handheld.component.login.bean.LoginBean;
import com.zhiyizhen.handheld.component.main.bean.EnumFieldBean;
import com.zhiyizhen.handheld.databinding.FragmentChargeRecordBinding;
import com.zhiyizhen.handheld.net.ServiceFactory;
import com.zhiyizhen.handheld.net.response.ResponseData;
import com.zhiyizhen.handheld.net.response.ResponseObserver;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class ChargeRecordFragment extends BaseFragment {
    private FragmentChargeRecordBinding binding;
    private ChargeRecordAdapter adapter;
    private String orderId;
    private List<EnumFieldBean> payStatusEnum;
    public static final int SHUYE = 1;
    public static final int LILIAO = 2;

    private int executeType = 1;

    public ChargeRecordFragment(int executeType) {
        this.executeType = executeType;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentChargeRecordBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    public void setData(String orderId, List<EnumFieldBean> payStatusEnum) {
        this.orderId = orderId;
        this.payStatusEnum = payStatusEnum;
        getChargeList();
        binding.emptyView.setVisibility(View.GONE);
        binding.recyclerView.setVisibility(View.VISIBLE);
    }

    public void setEmptyData() {
        binding.emptyView.setVisibility(View.VISIBLE);
        binding.recyclerView.setVisibility(View.GONE);
    }

    private void initView() {
        RecyclerViewUtil.configLVRecyclerView(getContext(), binding.recyclerView);
        adapter = new ChargeRecordAdapter(this.getContext());
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                getChargeList();
            }

            @Override
            public void onLoadMore() {
                getChargeList();
            }
        });
    }

    private void getChargeList() {
        LoginBean loginBean = DataCache.getLoginBean();
        if (loginBean == null) {
            return;
        }
        ServiceFactory.getService(IExecuteService.class)
                .getChargeList(loginBean.getToken(), loginBean.getClinicId(), orderId)
                .compose(RxJavaUtil.mainSchedulers())
                .as(this.bindLifecycle())
                .subscribe(new ResponseObserver<>() {
                    @Override
                    protected void onSuccess(ResponseData<List<ChargeRecordBean>> responseData) {
                        if (responseData != null) {
                            adapter.setPayStatusEnum(payStatusEnum);
                            adapter.setData(responseData.getData());
                        }
                    }

                    @Override
                    protected void onFinally() {
                        RecyclerViewUtil.loadComplete(binding.recyclerView, 1, true, true, adapter.getData().isEmpty());
                    }
                });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void addNewRecord(AddNewRecordEvent event) {
        if ((event.getRecordType() == AddNewRecordEvent.CHARGE_RECORD_SHUYE && executeType == SHUYE)
                || (event.getRecordType() == AddNewRecordEvent.CHARGE_RECORD_LILIAO && executeType == LILIAO)) {
            if (!TextUtils.isEmpty(orderId)) {
                getChargeList();
            }
        }
    }
}
